package unit14.bounce;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BounceFrame extends JFrame {
	private BallComponent comp;
	private static final int STEPS=1000;
	private static final int DELAY=3;
	
	public BounceFrame(){
		setTitle("Bounce");
		
		comp=new BallComponent();
		add(comp,BorderLayout.CENTER);
		JPanel buttonPanel=new JPanel();
		addButton(buttonPanel,"start",new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				addBall();
			}
		});
		addButton(buttonPanel, "Close", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		add(buttonPanel, BorderLayout.SOUTH);
		pack();
	}
	/**
	 * Adds a bouncing ball to the panel and makes it bounce 1,000 times
	 */
	protected void addBall() {
		// TODO Auto-generated method stub
		try {
			Ball ball=new Ball();
			comp.add(ball);
			for(int x=1;x<=STEPS;x++){
				ball.move(comp.getBounds());
				comp.paint(comp.getGraphics());
				Thread.sleep(DELAY);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * 
	 * @param c
	 * @param string
	 * @param actionListener
	 */
	private void addButton(Container c, String string, ActionListener actionListener) {
		// TODO Auto-generated method stub
		JButton button=new JButton(string);
		c.add(button);
		button.addActionListener(actionListener);
	}
}
